import "../../less/fillingTeeth/fillingTeeth.less";



globalClose();
$('#playSound')[0].play();

init();
function init() {
  let num = 3;
  let redaynum = 20;
  $(".ready-timer").html(redaynum);
  $(".time").html(num);
  $('.over-box').fadeIn();
  let timer1 = setInterval(function () {
    num--;
    if (num == -1) {
      clearInterval(timer1);
      $(".over-box").hide();
      let timer2 = setInterval(function () {
        redaynum--;
        if (redaynum == 0) {
          clearInterval(timer2);
          level();
          $(".report").show();
        }
        let str = redaynum < 10 ? "0" + redaynum : redaynum;
        $(".ready-timer").html(str);
      }, 1000);
    }
    $(".time").html(num);
  }, 1000);
}

// 默认属性

let activeAry = []
let teethList = $('.teeth')
let toothNum = 0;   // 补牙计数
let activeNum = 0;  //  当前牙齿在线数
let initCount = 4

// 重置
reset();
function reset() {
  activeAry = []
  toothNum = 0;
  $('.ready-tooth').html(toothNum)
  for (var i = 0; i < initCount; i++) {
    activeAry.push(rangeNum(0, 13))
  }
  activeAry.forEach(item => {
    teethAdd($(teethList[item]))
  })
  activeNum = activeAry.length
}


//  默认显示几颗牙齿


$("#tooth-arr").on("click", "img", function (event) {
  let that = $(this);
  teeth(that,'top')
});
$("#tooth-arr-bottom").on("click", "img", function (event) {
  let that = $(this,'bottom');
  teeth(that)
});

// 补牙公共

function teeth(self,postion) {
  console.log($('#toothSound'))
  $('#toothSound')[0].play();
  let curIndex = self.index();
  if (!self.hasClass('active')) {
    teethAdd(self,postion);
    toothNum += 1;
    activeNum += 1;
    $('.ready-tooth').text(toothNum);
    let maxCount = 6
    if (activeNum >= maxCount) {
      let range = rangeNum(2, 4)
      //  随机前面删除，还是最后面删除
      if (range > 2) {
        for (var i = teethList.length; i >= 0; i--) {
          let item = teethList[i]
          if ($(item).hasClass('active') && activeNum > maxCount - range && i != curIndex && i != curIndex + 6) {
            fallTeeth(item)
          }
        }
      } else {
        teethList.each((i, item) => {
          // img 必须有active, 牙齿在线数值大于最低数值，不删除当前curIndex的，和对应上面的
          if ($(item).hasClass('active') && activeNum > maxCount - range && i != curIndex && i != curIndex + 6) {
            fallTeeth(item)
          }
        });
      }
    }
  } else {
    // fallTeeth(self)
  }

}

//  排位评级
level();
function level() {
  let imgEl = $('.report .report_level')
  let index = 0;
  $(imgEl).hide()
  if (toothNum <= 20) {
    index = 0;
  } else if (toothNum <= 30) {
    console.log(3232)
    index = 1;
  } else if (toothNum <= 40) {
    console.log(323)
    index = 2;
  } else {
    index = 3;
  }
  $('.report_teeth_number').text(toothNum);
  $(imgEl[index]).show();
}
//牙齿添加
function teethAdd(self,position) {
  self.addClass('active')
  let anName = position == 'top'? 'handRotate':'handRotate2'
  self.css("animation", `${anName} .5s forwards`);
}

//脱落牙齿
function fallTeeth(item) {
  $(item).css("animation", "topHideTeeth 1.5s");
  $(item).removeClass('active')
  activeNum -= 1;
}
// 随机数
function rangeNum(start, end) {
  return Math.floor(Math.random() * (end - start) + start)
}

// 重置
$('.report .reset').on('click', function () {
  $('#tooth-arr-bottom img').removeClass('active').css("animation", "topHideTeeth 0s");
  $('#tooth-arrimg img').removeClass('active').css("animation", "topHideTeeth 0s");
  reset();
  $('.report').fadeOut();
  init();
})

// 流量密码
$('.report .openLink').on('click', function () {
  //linkTo
})